const express = require('express')

const router = express.Router()

const multer = require('multer')

const path = require('path')

const xy = require('../../../utils/xy')

const uploadsPath = path.join(__dirname, '../', '../', '../', 'uploads')

const upload = multer({ 
    storage: multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, uploadsPath)
        },
        filename: function (req, file, cb) {
            // console.log(111, file);
            let filename = +new Date() + Math.round(Math.random() * 8999999999999999)
            filename = filename.toString(36) + filename
            cb(null, filename + path.extname(file.originalname))
        }
    })
})

router.post('/', upload.single('dishimg'), (req, resp) => {
    // console.log(222, req.file);
    xy(resp, 200, '图片上传成功！', {
        filepath: 'uploads/' + req.file.filename
    })
})

module.exports = router